//
//  BaseTabbarController.m
//  YOBO
//
//  Created by 王根强 on 2017/3/9.
//  Copyright © 2017年 walemaishan. All rights reserved.
//


#import "BaseTabbarController.h"
#import "BaseNavigationController.h"
#import "HomeViewController.h"
#import "VideoViewController.h"
#import "VIPViewController.h"
#import "MineViewController.h"
@implementation BaseTabbarController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.tabBar.backgroundImage = [self imageWithColor:[UIColor colorWithRed:1 green:1 blue:1 alpha:1]];
    self.tabBar.shadowImage = [UIImage new];
    [self setTabBarVC];
//    self.tabBar.backgroundImage = [UIImage imageNamed:@"tabbar_backimage"];
//    self.tabBar.selectedItem.imageInsets = UIEdgeInsetsMake(0, 0, 0, 0);
}

- (UIImage *)imageWithColor:(UIColor *)color {

    CGRect rect = CGRectMake(0.0f,0.0f, 1.0f,1.0f);
    UIGraphicsBeginImageContext(rect.size);
    CGContextRef context =UIGraphicsGetCurrentContext();
    CGContextSetFillColorWithColor(context, [color CGColor]);
    CGContextFillRect(context, rect);
    UIImage *image =UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    return image;
}

// 初始化所有子控制器
- (void)setTabBarVC {
    [self setTabBarChildController:[[HomeViewController alloc] init] title:@"首页" image:@"HomeNormal" selectImage:@"HomeSelect"];
    [self setTabBarChildController:[[VideoViewController alloc]init] title:@"影像馆" image:@"VideoNormal" selectImage:@"VideoSelect"];
    [self setTabBarChildController:[[VIPViewController alloc] init] title:@"VIP下载专区" image:@"VIPNormal" selectImage:@"VIPSelect"];
    [self setTabBarChildController:[[MineViewController alloc] init] title:@"我的" image:@"MineNormal" selectImage:@"MineSelect"];
}

// 添加tabbar的子viewcontroller
- (void)setTabBarChildController:(UIViewController*)controller title:(NSString*)title image:(NSString*)imageStr selectImage:(NSString*)selectImageStr {
    BaseNavigationController* nav = [[BaseNavigationController alloc] initWithRootViewController:controller];
    nav.tabBarItem.title = title;
    //nav.tabBarController.view.backgroundColor = NAV_COLOR_RED;
    nav.tabBarItem.image = [[UIImage imageNamed:imageStr]imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
    nav.tabBarItem.selectedImage = [[UIImage imageNamed:selectImageStr]imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
    [nav.tabBarItem setTitleTextAttributes:@{NSFontAttributeName:[UIFont systemFontOfSize:12],NSForegroundColorAttributeName:RGB_VALUE(0x909090)} forState:UIControlStateNormal];
    [nav.tabBarItem setTitleTextAttributes:@{NSFontAttributeName:[UIFont systemFontOfSize:12],NSForegroundColorAttributeName:RGB_VALUE(0xDC0000)} forState:UIControlStateSelected];
    
    if (@available(iOS 13.0, *)) {
          // titColor就是选中的颜色
        self.tabBar.tintColor = RGB_VALUE(0xDC0000);
    //如果需要设置默认颜色可以使用setUnselectedItemTintColor来设置未选中颜色
        [self.tabBar setUnselectedItemTintColor:RGB_VALUE(0x909090)];
    } else {
          
      // 统一给所有的UITabBarItem设置文字属性
        UITabBarItem *item = [UITabBarItem appearance];
        [item setTitleTextAttributes:@{NSFontAttributeName:[UIFont systemFontOfSize:12],NSForegroundColorAttributeName:RGB_VALUE(0x909090)} forState:UIControlStateNormal];
        [item setTitleTextAttributes:@{NSFontAttributeName:[UIFont systemFontOfSize:12],NSForegroundColorAttributeName:RGB_VALUE(0xDC0000)} forState:UIControlStateSelected];
    }
    if ([controller isKindOfClass:[VideoViewController class]]) {
//        [controller viewDidLoad];
        VideoViewController *viewController = (VideoViewController *)nav.viewControllers.firstObject;
        [viewController loadViewIfNeeded]; // 让其调用viewdidload
    }
    [self addChildViewController:nav];
}

@end
